﻿//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd. 
//------------------------------------------------------------

using System;
using System.Data;
using System.Reflection;

namespace DotNet.Service
{
    using DotNet.Business;
    using DotNet.DbUtilities;
    using DotNet.IService;
    using DotNet.Model;
    using DotNet.Utilities;

    /// <summary>
    /// LogService
    /// 日志服务
    /// 
    /// 修改纪录
    /// 
    ///		2008.03.23 版本：1.0 JiRiGaLa 创建。 
    /// 
    /// 版本：1.0
    ///
    /// <author>
    ///		<name>JiRiGaLa</name>
    ///		<date>2008.03.23</date>
    /// </author> 
    /// </summary>
    public class LogService : System.MarshalByRefObject, ILogService
    {
        /// <summary>
        /// 用户中心数据库连接
        /// </summary>
        private readonly string UserCenterDbConnection = BaseConfiguration.Instance.UserCenterDbConnection;

        #region public void WriteLog(BaseUserInfo userInfo, string processId, string processName, string methodName) 写入日志
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="processID">服务</param>
        /// <param name="processName">服务名称</param>
        /// <param name="methodName">操作名称</param>
        /// <param name="description">备注</param>
        public void WriteLog(BaseUserInfo userInfo, string processId, string processName, string methodName)
        {
            // 写入调试信息
            #if (DEBUG)
                int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                BaseLogManager.Instance.Add(dbHelper, userInfo, processId, processName, methodName);
            }
            catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }
            
            // 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif
        }
        #endregion

        #region public DataTable GetLogGeneral(BaseUserInfo userInfo) 获取日志
        /// <summary>
        /// 获取日志
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public DataTable GetLogGeneral(BaseUserInfo userInfo)
        {
            // 写入调试信息
            #if (DEBUG)
                int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif
            DataTable dataTable = new DataTable(BaseLogTable.TableName);
            
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            BaseUserManager userManager = new BaseUserManager(dbHelper, userInfo);
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                if (userInfo.IsAdministrator)
                {
                    dataTable = userManager.GetDT();
                }
                else
                {
                    BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(dbHelper, userInfo);
                    string[] userIds = resourcePermissionScopeManager.GetUserIds(userInfo.Id, "SystemAdmin");
                    dataTable = userManager.GetDTByIds(userIds);
                }
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }

            // 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif
            return dataTable;
        }
        #endregion

        #region public DataTable ResetVisitInfo(BaseUserInfo userInfo, string[] ids) 重置访问情况
        /// <summary>
        /// 重置访问情况
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">日志主键</param>
        /// <returns>数据表</returns>
        public DataTable ResetVisitInfo(BaseUserInfo userInfo, string[] ids)
        {
            // 写入调试信息
            #if (DEBUG)
                int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif
            DataTable dataTable = new DataTable(BaseLogTable.TableName);

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                dbHelper.BeginTransaction();
                BaseUserManager userManager = new BaseUserManager(dbHelper, userInfo);
                // 重置访问情况
                userManager.ResetVisitInfo(ids);
                // 获取列表
                dataTable = userManager.GetDT();
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
                dbHelper.CommitTransaction();
            }
            catch (Exception ex)
            {
                dbHelper.RollbackTransaction();
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }

            // 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif
            return dataTable;
        }
        #endregion

        #region public DataTable GetDTByDate(BaseUserInfo userInfo, string beginDate, string endDate) 按日期获取日志
        /// <summary>
        /// 按日期获取日志
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="beginDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByDate(BaseUserInfo userInfo, string beginDate, string endDate)
        {
            // 写入调试信息
            #if (DEBUG)
                int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif
            DataTable dataTable = new DataTable(BaseLogTable.TableName);

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                BaseLogManager logManager = new BaseLogManager(dbHelper, userInfo);
                if (userInfo.IsAdministrator)
                {
                    dataTable = logManager.GetDTByDate(string.Empty, string.Empty, beginDate, endDate);
                }
                else
                {
                    BaseResourcePermissionScopeManager BaseResourcePermissionScopeManager = new BaseResourcePermissionScopeManager(dbHelper, userInfo);
                    string[] staffIds = BaseResourcePermissionScopeManager.GetUserIds(userInfo.Id, "SystemAdmin");
                    dataTable = logManager.GetDTByDateByUserIds(staffIds, string.Empty, string.Empty, beginDate, endDate);
                }                
                // 添加访问日志
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }

            // 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif
            return dataTable;
        }
        #endregion

        #region public DataTable GetDTByModule(BaseUserInfo userInfo, string processId, string beginDate,string endDate) 按服务获取日志
        /// <summary>
        /// 按服务获取日志
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="processID">服务名称</param>
        /// <param name="beginDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByModule(BaseUserInfo userInfo, string processId, string beginDate, string endDate)
        {
            // 写入调试信息
            #if (DEBUG)
                int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif
            DataTable dataTable = new DataTable(BaseLogTable.TableName);

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                BaseLogManager logManager = new BaseLogManager(dbHelper, userInfo);
                if (userInfo.IsAdministrator)
                {
                    dataTable = logManager.GetDTByDate(BaseLogTable.FieldProcessId, processId, beginDate, endDate);
                }
                else
                {
                    BaseResourcePermissionScopeManager resourcePermissionScopeManager = new BaseResourcePermissionScopeManager(dbHelper, userInfo);
                    string[] userIds = resourcePermissionScopeManager.GetUserIds(userInfo.Id, "SystemAdmin");
                    dataTable = logManager.GetDTByDateByUserIds(userIds, BaseLogTable.FieldProcessId, processId, beginDate, endDate);
                }
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }

            // 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif
            return dataTable;
        }
        #endregion

        #region public DataTable GetDTByUser(BaseUserInfo userInfo, string userId, string beginDate, string endDate) 按职员获取日志
        /// <summary>
        /// 按职员获取日志
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">用户主键</param>
        /// <param name="beginDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByUser(BaseUserInfo userInfo, string userId, string beginDate, string endDate)
        {
            // 写入调试信息
            #if (DEBUG)
                int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif
            DataTable dataTable = new DataTable(BaseLogTable.TableName);

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                BaseLogManager logManager = new BaseLogManager(dbHelper, userInfo);
                dataTable = logManager.GetDTByDate(BaseLogTable.FieldUserId, userId, beginDate, endDate);
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }

            // 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif
            return dataTable;
        }
        #endregion

        #region public int Delete(BaseUserInfo userInfo, string id) 删除日志
        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主键</param>
        /// <returns>影响行数</returns>
        public int Delete(BaseUserInfo userInfo, string id)
        {
            // 写入调试信息
            #if (DEBUG)
                int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif
            int returnValue = 0;

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                BaseLogManager logManager = new BaseLogManager(dbHelper, userInfo);
                returnValue = logManager.Delete(id);
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }
            
            // 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif
            return returnValue;
        }
        #endregion

        #region public int BatchDelete(BaseUserInfo userInfo, string[] ids) 批量删除日志
        /// <summary>
        /// 批量删除日志
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>影响行数</returns>
        public int BatchDelete(BaseUserInfo userInfo, string[] ids)
        {
            // 写入调试信息
            #if (DEBUG)
                int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif
            int returnValue = 0;

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                BaseLogManager logManager = new BaseLogManager(dbHelper, userInfo);
                returnValue = logManager.BatchDelete(ids);
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }

            // 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif
            return returnValue;
        }
        #endregion

        #region public DataTable Truncate(BaseUserInfo userInfo) 清除日志
        /// <summary>
        /// 清除日志
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public DataTable Truncate(BaseUserInfo userInfo)
        {
            // 写入调试信息
            #if (DEBUG)
                int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif
            DataTable dataTable = new DataTable(BaseLogTable.TableName);

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                BaseLogManager logManager = new BaseLogManager(dbHelper, userInfo);
                logManager.Truncate();
                dataTable = logManager.GetDT();
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }

            // 写入调试信息
            #if (DEBUG)
                BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif
            return dataTable;
        }
        #endregion
    }
}